//
//  ShoppingSellerCtr.swift
//  smosky
//
//  Created by zhuxietong on 2018/6/20.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import UIKit
import soapp
import AsyncDisplayKit
import Eelay

extension ShoppingSellerCtr{
    public static var top_height = 245.co
    
    func change(filterValues:NSMutableDictionary){
        updateListWith()
    }
    func updateListWith() {
        let info = self.filterValues.mutable_dictionary
        switch info["name",""] {
        case "cate":
            filter.cate = filterValues["value"] as? [String:Any]
        case "searchKey":
            filter.searchKey = filterValues["value"] as? String
        case "order":
            filter.order = ["order":info["orderKey",""],"by":info["by",""]]
        default:
            break
        }
        paginator.parameters = filter.params + baseParams
        co_delay(delay: 0.25) {[weak self] () in
            guard let ws = self else{return}
            ws.hud.show().mode(.indeterminate)

            ws.loadMore(page: ws.paginator.page_begin)
        }
    }
}


open class ShoppingSellerCtr: NavChangeASCollectionContainer {
    
    lazy var hud:HUD = {
        return HUD(at: view)
    }()
    public var filterValues = [String:Any]()
    {
        didSet{
            updateListWith()
        }
    }
    
    open var filterMenuNode:SellerFilterMenuNode.Type{
        get{
            return SellerFilterMenuNode.self
        }
    }
    
    override open var sections: TP.section{
        get{
            let sec:TP.section = [
                [
                    ["cellID":"header"],
                ],
                [
                    ["header":"YES"]
                ],
                [
                ],
            ]
            return sec
        }
    }
    override open var selector: TP.selector{
        get{
            let sel:TP.selector = [
                "cellID<header>":TrSellerHeadNode.self,
                "cellID<empty>":EmptyCell.self,
                "header<YES>":filterMenuNode
                ] + Goods.cellSelector
            return sel
        }
    }
    
    open var ID:String{
        get{
            return mo.model["id",""]
        }
    }
    open var filter:Goods.Filter = Goods.Filter()
    
    public var header:BaseSellerHeader? = nil
    
    public var headerNode:SellerHeadNode? = nil
    
    public var baseParams:[String:Any]{
        get{
            let params = ["merchid":ID]
            return params
        }
    }
    
    override open func viewDidLoad() {
        
        self.page_load_section = "section1"
        content = ASTermsContainer.list(
            net: JoTask().get.url(_SURL.商品列表.url),
            node: "list",
            emptyMsg: "",
            params: baseParams,
            cellType: JoCellNode.self,
            detailType: nil,
            test: false
        )
        configFilterMenu()
        super.viewDidLoad()
        self.paginator.type = .bottom
        //        tableView.buildDynamicHeight(height: 260)
        //        load(sections: sections, selector: selector)
        title = "卖家店铺".loc
        mo_request_send()
        buildBackBtChangeNavSupport()
        collectionNode.backgroundColor = UIColor.white
        navChangeOffsetY = ShoppingSellerCtr.top_height - (2.0 * TP.topHeight)
        reload_page()

        
    }
    
    open func configFilterMenu(){
        

        flowLayout.sectionHeadersPinToVisibleBounds = true
        collectionNode.registerSupplementaryNode(ofKind: UICollectionView.elementKindSectionHeader)

    }
    
    
    override open func load(page: Int, succeed: Bool, list: [Any], response: RootData?, status: PageDataStatus, scrollView: UIScrollView) {
        hud.hide(animated: true, delay: 0)
        super.load(page: page, succeed: succeed, list: list, response: response, status: status, scrollView: scrollView)
//        if let menu = (collection_config as? HeaderConfig)?.menuNode.menu.menu{
//            menu.mo.model = mo.detail
//        }
    }
    
    
    override open func mo_request_send() {
        
        weak var wself = self
        //        loadingV.loading()
        JoTask().get.log.params(["id":ID])._surl(.商户信息).json_handle { (status, msg, obj, resp) in
//            print("++++++++++++++++IIII|\(obj)|")
            wself?.loadingV.dismiss()
            if status.isSuccess{
                if let dict = DataNode<NSMutableDictionary>.path(node: "merch", obj){
                    wself?.mo.detail = dict
                    return
                }
            }
            }.run()
    }
    
    
    override open func mo_detail_reload() {
        self.headerNode?.mo.model = mo.detail
//        if let menu = (collection_config as? HeaderConfig)?.menuNode.menu.menu{
//            menu.mo.model = mo.detail
//        }
    }
    
    
    override open func collectionNode(_ collectionNode: ASCollectionNode, nodeForItemAt indexPath: IndexPath) -> ASCellNode {
        let node = super.collectionNode(collectionNode, nodeForItemAt: indexPath)
        if let headerNode = node as? SellerHeadNode
        {
            headerNode.mo.model = mo.detail
            self.headerNode = headerNode
            header = headerNode.heaer?.view as? BaseSellerHeader

        }
        return node
    }
    
    open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        if section == 0{
            return .zero
        }
        else{
            return Goods.listInset
        }
    }
    
    override open func scrollViewDidScroll(_ scrollView: UIScrollView) {
        super.scrollViewDidScroll(scrollView)
        if self.appearStatus != .didAppear
        {
            return
        }
        
        header?.scrollViewDidScroll(scrollView: scrollView)
    }
    
//    func collectionNode(_ collectionNode: ASCollectionNode, sizeRangeForHeaderInSection section: Int) -> ASSizeRange {
//        if section == 1
//        {
//            return ASSizeRange(min: CGSize(width: Swidth, height: 40),
//                               max: CGSize(width: Swidth, height: 40))
//        }
//        return ASSizeRange(min: CGSize(width: Swidth, height: 0),
//                           max: CGSize(width: Swidth, height: 0))
//    }
    
    override open func collectionNode(_ collectionNode: ASCollectionNode, didSelectItemAt indexPath: IndexPath) {
        if indexPath.section == 1{
            guard let data = collectionNode.cellData(at: indexPath) else{return}
            Goods.seeDetail(obj: data, at: self)
        }
    }
    
    
    
    deinit {
    }
}

